begintownscript;

variables;
short creature_count = 0;
short choice = 0;
short pc_counter = 0;
short lead_char = 0;
string roach_jump;

body;

beginstate 0; //INIT_STATE

	//Guards
	add_range_to_group(6,10,1);
	add_range_to_group(13,17,1);
	set_level(1001,15);
	set_name(1001,"Guard");

	add_range_to_group(6,17,2);

	enable_add_chars(1);
	set_crime_tolerance(2);
	set_name(18, "Commander Ylissa");
	set_level(18,35);
	set_name(19, "Sorkeli");
	set_level(19,22);

	//remove Sorkeli
	if(get_flag(5,7) == 1)
		erase_char(19);

	//remove statue terrains if town status is hostile
	if((town_status(2) == 2) || (town_status(3) == 2)) {
		make_town_hostile(3);  
		set_terrain(17,26,0);
		set_terrain(31,20,0);
		activate_hidden_group(1);
		force_instant_terrain_redraw();
		set_mobility(11,2);
		set_mobility(12,2);
		set_level(11,20);
		set_level(12,20);
	}

	//Dialogue pics
	set_char_dialogue_pic(19,1921,0);
	set_char_dialogue_pic(18,1959,0);

break;


beginstate 1; //EXIT_STATE

break;


beginstate 2; //START_STATE
	pc_counter = 0;
	while(char_ok(pc_counter) == 0) {
		pc_counter = pc_counter + 1;
		}
	lead_char = pc_counter;

break;


beginstate 10; //View from west windows
	if(get_flag(3,17) == 0) {
		reset_dialog();

		if(get_flag(2,12) != 1) {
			add_dialog_str(0,"This side of the fort overlooks the entire lake, and you can even catch a glimpse of the falls at the far north, with that strange stone bridge spanning a stretch of them.",0);
			add_dialog_str(1,"From here, you can see just how far the fort is above the lake. It looks like a pretty sheer drop.",0);
			}
		else {
			add_dialog_str(0,"This view of the lake is by far superior to the one downstairs. You can see a bit farther as well.",0);
			add_dialog_str(1,"There's also a nice breeze up here, which you stop to enjoy for a moment.",0);
			}
		run_dialog(1);
		
		set_flag(3,17,1);
	}
break;


beginstate 15; //Entering the main upper hall
	if(get_flag(3,2) == 0) {
		reset_dialog();
		add_dialog_str(0,"You step out into the rather breezy hallway, and are struck by the relative opulence of the decorations. A long pool of crystal clear water dominates the center of the hall, and you can see a few fish darting around below the surface.",0);
		add_dialog_str(1,"You can see a pair of statues at opposite ends of the room. Flickering braziers cast somewhat uneven light around the hall, making the statues' shadows dance. It almost makes them look like they're alive.",0);
		add_dialog_str(2,"Suddenly, from the doors to the west, you hear a loud magical _whoosh,_ followed by a short series of pops. Slowly, a smell something like burning hair reaches you, and you try not to gag.",0);
		add_dialog_str(4,"You get the vague impression that the fort has some mages on staff.",0);
		run_dialog(1);
		set_flag(3,2,1);
	}
break;


beginstate 16; //west statue
	reset_dialog();
	add_dialog_str(0, "This statue flawlessly captures the frailty of a bureaucratic-looking old man. It looks as if even the statue might fall over. You can't help but wonder what he did that merited the statue.", 0);
	add_dialog_str(1, "Then you notice the plaque at the bottom of the statue:",0);
	add_dialog_str(2, "Matthias Colt, 720-813. Funded the renovation of Fort Galima.", 35);
	add_dialog_str(3, "Somehow, you're not surprised.",0);
	run_dialog(1);
break;


beginstate 17; //east statue
	reset_dialog();
	add_dialog_str(0, "This statue depicts an armor-clad woman shouting something at someone and brandishing a long rapier. She looks intimidating, to put it mildly.", 0);
	add_dialog_str(1, "The plaque at the bottom of the statue reads:",0);
	add_dialog_str(2, "Ylissa Brecke, commander of Fort Galima, 837- .", 35);
	run_dialog(1);
	set_flag(3,3,1);
break;


beginstate 18; //braziers
	if(get_flag(3,7) == 0) {
		message_dialog("You look in this brazier, expecting to see a few logs and a bunch of ashes. Instead, there's just a flame. No fuel, just fire.","It must be magically lit. You're beginning to wonder about this fort's budget...");
		set_flag(3,7,1);
		}
break;


beginstate 20; //Upper living quarters
	if(get_flag(3,8) == 0) {
		reset_dialog();
		add_dialog_str(0,"Compared to most of the Empire forts you've seen, Fort Galima is unusually well-built. However, the soldiers' quarters are downright depressing.",0);
		add_dialog_str(1,"The beds look like they haven't been made in weeks, and there's a decidedly stale smell in the room. You doubt that anyone bothers to clean on a regular basis.",0);
		run_dialog(1);
		set_flag(3,8,1);
	}
break;


beginstate 21; //cockroach in the dresser
	if(get_flag(3,1) == 0) {
		reset_dialog();
		if(char_on_loc(30,32) == -1)
			add_dialog_str(0,"Examining this dresser, you're somehow not surprised to find a rather large cockroach. You're also not surprised when it jumps out and attacks you.",0);
		else {
			clear_buffer();
			append_string("Examining this dresser, you're somehow not surprised to find a rather large cockroach. However, ");
			append_char_name(char_on_loc(30,32));
			append_string(" is rather surprised when the cockroach leaps onto them.");
			get_buffer_text(roach_jump);
			add_dialog_str(0, roach_jump, 0);
			set_char_status(char_on_loc(30,32),20,20,1,1);
			set_char_status(char_on_loc(30,32),30,40,1,1);
		}
		run_dialog(1);
		activate_hidden_group(2);
		relocate_character(24,30,32);
		set_level(24,12);
		set_flag(3,1,1);
	}
break;


beginstate 25; //Commander Ylissa's office
	if(get_flag(3,8) == 0) {
		reset_dialog();
		add_dialog_str(0,"At first, this looks like every other Empire office you've ever seen, complete with paperwork-laden bookshelves. Then you see the woman dressed in full chainmail sitting at the desk, scribbling out some notes.",0);
		add_dialog_str(1,"You're not sure, but it looks like she's watching you out of the corner of her eye.",0);
		run_dialog(1);
		set_flag(3,8,1);
	}
break;


beginstate 26; //Secret passage, only available if Ylissa's dead (specobj)
	if(char_ok(18) == 1) {
		reset_dialog();
		add_dialog_str(0,"As you scan the titles on this bookshelf, you notice a slight breeze coming from behind some books. You'd investigate further, but you're honestly afraid to mess with anything while Commander Ylissa is in the room.",0);
		run_dialog(1);
	}
	else {
		reset_dialog();
		add_dialog_str(0,"Now that the commander is dead, you can search the bookshelves without worry. You feel the strange breeze again, and find its source in a dull-looking, leather-bound tome titled _Empire Military History_.",0);
		add_dialog_str(1,"You pull the book out of the way, and are rewarded by the sight of the wall swinging into a new room.",0);
		add_dialog_str(2,"When you get a good look at the room, you realize that you've stumbled into the commander's personal living quarters. The room is sparsely furnished, but it's surprisingly clean, setting it apart from the rest of the fort.",0);
		run_dialog(1);
		set_terrain(18,31,234);
		set_terrain(18,30,8);
		set_terrain(19,31,0);
		play_sound(45);
		force_instant_terrain_redraw();
	}
break;


beginstate 27; //journal in Ylissa's quarters

	if(get_flag(3,15) == 0) {
		reset_dialog();
		add_dialog_str(0,"As you open this box, you find a heavy leather-bound book at the top. Curiosity overcomes you, and you flip to the last page with writing on it.",0);
		add_dialog_str(1,"...I cannot believe our position. We are being threatened with the same weapons we are trying to create. I fear that if we cannot find these rebels and disable them, the situation will escalate beyond our control.",10);
		if(get_flag(0,25) == 4)
			add_dialog_str(2,"Oh. That's... unfortunate. It seems that you've gone and attacked the wrong people. You suddenly get the urge to flee the fort.",0);
		else
			add_dialog_Str(2,"Well, that doesn't make much sense. You wonder what Ylissa meant.",0);
		run_dialog(1);
		set_flag(3,15,1);
		}

break;



beginstate 30; //Enter the lab
	if(get_flag(3,9) == 0) {
		reset_dialog();
		add_dialog_str(0,"As you enter the fort's lab, you are immediately assaulted by the smell of... well, whatever it is, it's unpleasant. It's almost like a blend of sweaty armor, rotten zombies, and vinegar.",0);
		add_dialog_str(1,"You're surprised to see that the lab has no actual windows. Fortunately, there seem to be vents set into the ceiling... otherwise, you wouldn't want to be in here for more than a few minutes.",0);
		add_dialog_str(2,"Once you recover from shock, you get a good look at the mages running this lab. Three tired-looking apprentices are rushing around the room, while one woman watches their progress, occasionally barking an order.",0);
		add_dialog_str(3,"None of them seem bothered by the smell, which you find hard to believe.",0);
		run_dialog(1);
		set_flag(3,9,1);
	}

	if((get_flag(5,7) == 1) && (get_flag(3,16) == 0)){
		reset_dialog();
		add_dialog_str(0,"As you return to the fort's lab, you notice that Sorkeli is absent. When you ask the apprentices where she is, they just shrug and say they don't know.",0);
		run_dialog(1);
		set_flag(3,16,1);
	}
break;


beginstate 31; //vat 'o junk, horrid smell, causes sickness
	if(get_flag(3,12) == 1) {
		message_dialog("After what Sorkeli said earlier, you're honestly afraid to get too close to this vat of... well, you don't really want to know what.","");
		end();
		}
	if(get_flag(3,10) == 0) {
		reset_dialog();
		add_dialog_str(0,"As you bend over to inspect this cauldron, you're hit by a wave of stench even fiercer than what you encountered at the door. You can't think of anything you've encountered that smells this bad...",0);
		add_dialog_str(1,"Everyone near the cauldron is immediately overcome by intense nausea.",0);
		run_dialog(1);
		set_flag(3,10,1);
		if(char_on_loc(16,19) != -1)
			set_char_status(char_on_loc(16,19),7,25,0,0);
		if(char_on_loc(16,20) != -1)
			set_char_status(char_on_loc(16,20),7,25,0,0);
		if(char_on_loc(16,18) != -1)
			set_char_status(char_on_loc(16,18),7,25,0,0);
		if(char_on_loc(17,18) != -1)
			set_char_status(char_on_loc(17,18),7,25,0,0);
		if(char_on_loc(18,18) != -1)
			set_char_status(char_on_loc(18,18),7,25,0,0);
		if(char_on_loc(17,20) != -1)
			set_char_status(char_on_loc(17,20),7,25,0,0);
		if(char_on_loc(18,20) != -1)
			set_char_status(char_on_loc(18,20),7,25,0,0);
		if((char_on_loc(18,19) < 7) && (char_on_loc(18,19) != -1))
			set_char_status(char_on_loc(18,19),7,25,0,0);
		if(char_on_loc(18,19) == 19)
			message_dialog("Surprisingly, Sorkeli doesn't seem to be affected by the stench. Perhaps she's been working here too long.","");
	}
	else
		message_dialog("There's no way you're getting near that cauldron again. Nothing could possibly convince you to do so. Ever.","");
break;


beginstate 32; // same as 31, just for the south vat
	if(get_flag(3,12) == 1) {
		message_dialog("After what Sorkeli said earlier, you're honestly afraid to get too close to this vat of... well, you don't really want to know what.","");
		end();
		}
	if(get_flag(3,10) == 0) {
		reset_dialog();
		add_dialog_str(0,"As you bend over to inspect this cauldron, you're hit by a wave of stench even fiercer than what you encountered at the door. You can't think of anything you've encountered that smells this bad...",0);
		add_dialog_str(1,"Everyone near the cauldron is immediately overcome by intense nausea.",0);
		run_dialog(1);
		set_flag(3,10,1);
		if(char_on_loc(16,21) != -1)
			set_char_status(char_on_loc(16,19),7,25,0,0);
		if(char_on_loc(16,20) != -1)
			set_char_status(char_on_loc(16,20),7,25,0,0);
		if(char_on_loc(16,22) != -1)
			set_char_status(char_on_loc(16,18),7,25,0,0);
		if(char_on_loc(17,22) != -1)
			set_char_status(char_on_loc(17,18),7,25,0,0);
		if(char_on_loc(18,22) != -1)
			set_char_status(char_on_loc(18,18),7,25,0,0);
		if(char_on_loc(17,20) != -1)
			set_char_status(char_on_loc(17,20),7,25,0,0);
		if(char_on_loc(18,20) != -1)
			set_char_status(char_on_loc(18,20),7,25,0,0);
		if(char_on_loc(18,21) != -1)
			set_char_status(char_on_loc(18,19),7,25,0,0);
	}
	else
		message_dialog("There's no way you're getting near that cauldron again. Nothing could possibly convince you to do so. Ever.","");
break;


beginstate 33; //not-quite-full-vat
	if(get_flag(3,12) == 1) {
		message_dialog("After what Sorkeli said earlier, you're honestly afraid to get too close to this vat of... well, you don't really want to know what.","");
		end();
		}
	else
		message_dialog("This vat doesn't have a lot of anything in it, but it still smells incredibly bad. You highly doubt that there's anything vital in it, but you're not very interested in making sure.","");
break;


//Stairs & ladders
beginstate 40; //Ladder, NW Tower
	if(is_combat() == 1) {
		block_entry(1);
		print_str_color("You can't use the trapdoor in combat mode.",1);
		end();
		}
	move_to_new_town(2,10,8);
break;

beginstate 41; //Ladder, SW Tower
	if(is_combat() == 1) {
		block_entry(1);
		print_str_color("You can't use the trapdoor in combat mode.",1);
		end();
		}
	move_to_new_town(2,10,39);
break;

beginstate 42; //Ladder, NE Tower
	if(is_combat() == 1) {
		block_entry(1);
		print_str_color("You can't use the trapdoor in combat mode.",1);
		end();
		}
	move_to_new_town(2,40,8);
break;

beginstate 43; //Ladder, SE Tower
	if(is_combat() == 1) {
		block_entry(1);
		print_str_color("You can't use the trapdoor in combat mode.",1);
		end();
		}
	move_to_new_town(2,40,39);
break;

beginstate 44; //Stairs, Central Tower
	if(is_combat() == 1) {
		block_entry(1);
		print_str_color("You can't use the trapdoor in combat mode.",1);
		end();
		}
	move_to_new_town(2,31,19);
break;
//End of Stairs & ladders



//Wheels changing the gates around
beginstate 50; //East wheel of south gate

	if(is_combat() == 1) {
		block_entry(1);
		print_str_color("You can't use the wheel in combat mode.",1);
		end();
		}

	if((get_flag(3,0) != 1) && (town_status(3) != 2)) {
		message_dialog("This wheel has a chain wrapped around it, which disappears through a hole in the floor. Your best guess is that it controls the gates to the fort.","You think about fiddling with the wheel, but a glare from the nearby guard makes you think better of it.");
		}
	else {
		reset_dialog();
		add_dialog_str(0, "This wheel has a chain wrapped around it, which disappears through a hole in the floor. Your best guess is that it controls the gates to the fort.", 0);
		add_dialog_str(1, "Now that you need to escape, it seems like a much better idea to fiddle with the gates. Do you turn the wheel?",0);
		add_dialog_choice(0,"Leave the wheel alone.");
		add_dialog_choice(1,"Turn the wheel.");
		if(run_dialog(1) == 2) {
			play_sound(99);
			print_str_color("You hear chains rattling beneath you.",2);
			if(get_flag(2,0) == 0)
				set_flag(2,0,1);
			else
				set_flag(2,0,0);
			}
	}
break;


beginstate 51; //West wheel of south gate

	if(is_combat() == 1) {
		block_entry(1);
		print_str_color("You can't use the wheel in combat mode.",1);
		end();
		}

	if((get_flag(3,0) != 1) && (town_status(3) != 2)) {
		message_dialog("This wheel has a chain wrapped around it, which disappears through a hole in the floor. Your best guess is that it controls the gates to the fort.","You think about fiddling with the wheel, but a glare from the nearby guard makes you think better of it.");
		}
	else {
		reset_dialog();
		add_dialog_str(0, "This wheel has a chain wrapped around it, which disappears through a hole in the floor. Your best guess is that it controls the gates to the fort.", 0);
		add_dialog_str(1, "Now that you need to escape, it seems like a much better idea to fiddle with the gates. Do you turn the wheel?",0);
		add_dialog_choice(0,"Leave the wheel alone.");
		add_dialog_choice(1,"Turn the wheel.");
		if(run_dialog(1) == 2) {
			play_sound(99);
			print_str_color("You hear chains rattling beneath you.",2);
			if(get_flag(2,0) == 0)
				set_flag(2,3,1);
			else
				set_flag(2,3,0);
			}
	}
break;


beginstate 52; //East wheel of north gate

	if(is_combat() == 1) {
		block_entry(1);
		print_str_color("You can't use the wheel in combat mode.",1);
		end();
		}

	if((get_flag(3,0) != 1) && (town_status(3) != 2)) {
		message_dialog("This wheel has a chain wrapped around it, which disappears through a hole in the floor. Your best guess is that it controls the gates to the fort.","You think about fiddling with the wheel, but a glare from the nearby guard makes you think better of it.");
		}
	else {
		reset_dialog();
		add_dialog_str(0, "This wheel has a chain wrapped around it, which disappears through a hole in the floor. Your best guess is that it controls the gates to the fort.", 0);
		add_dialog_str(1, "Now that you need to escape, it seems like a much better idea to fiddle with the gates. Do you turn the wheel?",0);
		add_dialog_choice(0,"Leave the wheel alone.");
		add_dialog_choice(1,"Turn the wheel.");
		if(run_dialog(1) == 2) {
			play_sound(99);
			print_str_color("You hear chains rattling beneath you.",2);
			if(get_flag(2,0) == 0)
				set_flag(2,1,1);
			else
				set_flag(2,1,0);
			}
	}
break;


beginstate 53; //West wheel of north gate

	if(is_combat() == 1) {
		block_entry(1);
		print_str_color("You can't use the wheel in combat mode.",1);
		end();
		}

	if((get_flag(3,0) != 1) && (town_status(3) != 2)) {
		message_dialog("This wheel has a chain wrapped around it, which disappears through a hole in the floor. Your best guess is that it controls the gates to the fort.","You think about fiddling with the wheel, but a glare from the nearby guard makes you think better of it.");
		}
	else {
		reset_dialog();
		add_dialog_str(0, "This wheel has a chain wrapped around it, which disappears through a hole in the floor. Your best guess is that it controls the gates to the fort.", 0);
		add_dialog_str(1, "Now that you need to escape, it seems like a much better idea to fiddle with the gates. Do you turn the wheel?",0);
		add_dialog_choice(0,"Leave the wheel alone.");
		add_dialog_choice(1,"Turn the wheel.");
		if(run_dialog(1) == 2) {
			play_sound(99);
			print_str_color("You hear chains rattling beneath you.",2);
			if(get_flag(2,0) == 0)
				set_flag(2,4,1);
			else
				set_flag(2,4,0);
			}
	}
break;


beginstate 54; //North wheel of east gate

	if(is_combat() == 1) {
		block_entry(1);
		print_str_color("You can't use the wheel in combat mode.",1);
		end();
		}

	if((get_flag(3,0) != 1) && (town_status(3) != 2)) {
		message_dialog("This wheel has a chain wrapped around it, which disappears through a hole in the floor. Your best guess is that it controls the gates to the fort.","You think about fiddling with the wheel, but a glare from the nearby guard makes you think better of it.");
		}
	else {
		reset_dialog();
		add_dialog_str(0, "This wheel has a chain wrapped around it, which disappears through a hole in the floor. Your best guess is that it controls the gates to the fort.", 0);
		add_dialog_str(1, "Now that you need to escape, it seems like a much better idea to fiddle with the gates. Do you turn the wheel?",0);
		add_dialog_choice(0,"Leave the wheel alone.");
		add_dialog_choice(1,"Turn the wheel.");
		if(run_dialog(1) == 2) {
			play_sound(99);
			print_str_color("You hear chains rattling beneath you.",2);
			if(get_flag(2,0) == 0)
				set_flag(2,2,1);
			else
				set_flag(2,2,0);
			}
	}
break;


beginstate 55; //South wheel of east gate

	if(is_combat() == 1) {
		block_entry(1);
		print_str_color("You can't use the wheel in combat mode.",1);
		end();
		}

	if((get_flag(3,0) != 1) && (town_status(3) != 2)) {
		message_dialog("This wheel has a chain wrapped around it, which disappears through a hole in the floor. Your best guess is that it controls the gates to the fort.","You think about fiddling with the wheel, but a glare from the nearby guard makes you think better of it.");
		}
	else {
		reset_dialog();
		add_dialog_str(0, "This wheel has a chain wrapped around it, which disappears through a hole in the floor. Your best guess is that it controls the gates to the fort.", 0);
		add_dialog_str(1, "Now that you need to escape, it seems like a much better idea to fiddle with the gates. Do you turn the wheel?",0);
		add_dialog_choice(0,"Leave the wheel alone.");
		add_dialog_choice(1,"Turn the wheel.");
		if(run_dialog(1) == 2) {
			play_sound(99);
			print_str_color("You hear chains rattling beneath you.",2);
			if(get_flag(2,0) == 0)
				set_flag(2,5,1);
			else
				set_flag(2,5,0);
			}
	}
break;